package adapters;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.sql.Time;
import java.util.List;

import beans.LogBean;
import butterknife.BindView;
import custom.BaseRecyclerAdapter;
import utils.FormateUtils;
import utils.TimeUtil;

/**
 * Created by Administrator on 2018/5/5.
 */

public class LogAdapter extends BaseRecyclerAdapter<LogAdapter.LogViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<LogBean.LogDetailBean> list;
    private boolean isLoading = true;
    public LogAdapter(Context mContext, List<LogBean.LogDetailBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    public void setLoading(boolean isLoading){
        this.isLoading = isLoading;
    }

    @Override
    public LogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogViewHolder logViewHolder = new LogViewHolder(inflater.inflate(R.layout.adapter_log, parent, false));
        return logViewHolder;
    }

    public List<LogBean.LogDetailBean> getList() {
        return list;
    }

    public void setList(List<LogBean.LogDetailBean> list) {
        this.list = list;
    }

    @Override
    public void onBindViewHolder(LogViewHolder viewHolder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            viewHolder.content.setVisibility(View.VISIBLE);
            viewHolder.footer.setVisibility(View.GONE);
            LogBean.LogDetailBean logBean = list.get(position);

            if (TextUtils.isEmpty(logBean.getName()) || "应急开门".equals(logBean.getName())) {
                //应急开门,改成红色
                viewHolder.tvName.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvDepartment.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvRole.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvCabinetNumber.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvOperate.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvNumber.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));
                viewHolder.tvTime.setTextColor(mContext.getResources().getColor(R.color.emergency_open_red));

                viewHolder.tvName.setText("应急开门");
                viewHolder.tvDepartment.setText("无");
                viewHolder.tvRole.setText("无");
            } else {
                viewHolder.tvName.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvDepartment.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvRole.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvCabinetNumber.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvOperate.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvNumber.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));
                viewHolder.tvTime.setTextColor(mContext.getResources().getColor(R.color.table_head_text_color));

                viewHolder.tvName.setText(logBean.getName());
                viewHolder.tvDepartment.setText(logBean.getDepartment());
                viewHolder.tvRole.setText(logBean.getRoleInfo());
            }
            viewHolder.tvCabinetNumber.setText(logBean.getLogicNumber() + "号柜");
            String operate = logBean.getOperate();
            if (operate.equals("出柜")) {
                operate = "取出";
            } else if (operate.equals("入柜")) {
                operate = "放入";
            }
            viewHolder.tvOperate.setText(operate);
            viewHolder.tvNumber.setText(String.valueOf(logBean.getCount()));

            viewHolder.tvTime.setText(TimeUtil.getTime(logBean.getSubTime()));

            if (position % 2 == 0) {
                viewHolder.linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
            } else {
                viewHolder.linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            }
        }else {
            viewHolder.content.setVisibility(View.GONE);
            viewHolder.footer.setVisibility(View.VISIBLE);
            if (isLoading) {
                viewHolder.footerLoading.setVisibility(View.VISIBLE);
                viewHolder.footerText.setText("加载中 . . .");
                viewHolder.footerLoading.setBackgroundResource(R.drawable.loading);
                AnimationDrawable animationDrawable = (AnimationDrawable) viewHolder.footerLoading.getBackground();
                animationDrawable.start();
            }else {
                viewHolder.footerLoading.setVisibility(View.GONE);
                viewHolder.footerText.setText("没有更多数据了!");
            }
        }
    }

    public void setData(List<LogBean.LogDetailBean> list) {
        this.list.clear();
        this.list.addAll(list);
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 0 : list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < list.size()) {
            return 0;
        }
        return 1;//默认返回表示是尾部的item
    }

    class LogViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_adapter_log_name)
        TextView tvName;
        @BindView(R.id.tv_adapter_log_department)
        TextView tvDepartment;
        @BindView(R.id.tv_adapter_log_role)
        TextView tvRole;
        @BindView(R.id.tv_adapter_log_cabinet_number)
        TextView tvCabinetNumber;
        @BindView(R.id.tv_adapter_log_operate)
        TextView tvOperate;
        @BindView(R.id.tv_adapter_log_number)
        TextView tvNumber;
        @BindView(R.id.tv_adapter_log_time)
        TextView tvTime;
        @BindView(R.id.ll_adapter_log)
        LinearLayout linearLayout;
        @BindView(R.id.ll_log_content)
        LinearLayout content;
        @BindView(R.id.rl_log_footer)
        RelativeLayout footer;
        @BindView(R.id.tv_footer_text)
        TextView footerText;
        @BindView(R.id.iv_footer_loading)
        ImageView footerLoading;

        public LogViewHolder(View itemView) {
            super(itemView);
        }
    }
}
